﻿@model IList<Shop.Wares.Models.WaresCategory>
@{
    //类别管理类
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>类别管理</title>
    <link href="~/Content/BootStrap/css/bootstrap.min.css" rel="stylesheet" />
    <link href="~/Content/BootStrap/css/bootstrap-theme.min.css" rel="stylesheet" />
    <link href="~/Content/BootStrap/css/docs.min.css" rel="stylesheet" />
    <link href="~/bootstrapvalidator-master/dist/css/bootstrapValidator.min.css" rel="stylesheet" />

    <script src="~/Scripts/jquery-1.10.2.min.js"></script>
    <script src="~/Content/BootStrap/js/bootstrap.min.js"></script>
    <script src="~/Content/BootStrap/js/docs.min.js"></script>
    <script src="~/bootstrapvalidator-master/dist/js/bootstrapValidator.min.js"></script>
    <script src="~/bootstrapvalidator-master/dist/js/language/zh_CN.js"></script>
</head>
<body>
    <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#AddRole">
        添加类别
    </button>
    @*添加弹出层*@
    <div class="modal fade bs-example-modal-lg" id="AddRole" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog modal-lg" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="myModalLabel">添加类别</h4>
                </div>
                <form id="defaultForm" method="post" class="form-horizontal" action="">
                    <div class="modal-body">
                        <div class="form-group">
                            <label class="col-lg-3 control-label">类别名</label>
                            <div class="col-lg-5">
                                <input type="text" class="form-control" name="WaresCategoryName" id="WaresCategoryName" placeholder="类别名" />
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-lg-3 control-label">类别描述</label>
                            <div class="col-lg-5">
                                <input type="text" class="form-control" name="Describe" id="Describe" placeholder="类别描述(可以不填)" />
                            </div>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <input type="button" class="btn btn-default" data-dismiss="modal" value="关闭" />
                        <input type="button"  class="btn btn-primary" value="确定" id="add"/>
                    </div>
                </form>
            </div>
        </div>
    </div>
    <table class="table table-bordered table-striped table-hover">
        <tr>
            <td>类别ID</td>
            <td>类别名</td>
            <td>类别描述</td>
            <td>操作</td>
        </tr>
        @foreach (var item in Model)
        {
            <tr>
                <td>@item.CID</td>
                <td>@item.CName</td>
                <td>@item.Describe</td>
                <td>
                    <a href="SonWaresCategory-@item.CID" class="btn btn-default">管理子类别</a>
                    <input type="button" class="btn btn-default" value="修改" onclick="popupdate('@(item.CID)','@(item.CName)','@(item.Describe)','@(item.Createdate)')"/>
                    <input type="button" class="btn btn-default" value="删除" onclick="del('@(item.CID)')"/>
                    <input type="button" class="btn btn-default" value="置顶" onclick="stick('@(item.CID)')"/>
                </td>
            </tr>
        }
    </table>
    @*修改弹出层*@
    <div class="modal fade bs-example-modal-lg" id="UpdateRole" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog modal-lg" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="myModalLabel">修改类别</h4>
                </div>
                <form id="UdefaultForm" method="post" class="form-horizontal" action="">
                    <div class="modal-body">
                        <div class="form-group">
                            <label class="col-lg-3 control-label">类别名</label>
                            <div class="col-lg-5">
                                <input type="text" class="form-control" name="UWaresCategoryName" id="UWaresCategoryName" placeholder="类别名" />
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-lg-3 control-label">类别描述</label>
                            <div class="col-lg-5">
                                <input type="text" class="form-control" name="UDescribe" id="UDescribe" placeholder="类别描述(可以为空)" />
                            </div>
                        </div>
                        <input type="text" hidden="hidden" id="CID" name="CID" />
                        <input type="text" hidden="hidden" id="Createdate" name="Createdate" />
                        <input type="text" hidden="hidden" id="YCName" name="YCName" />
                    </div>
                    <div class="modal-footer">
                        <input type="button" class="btn btn-default" data-dismiss="modal" value="关闭" />
                        <input type="button" class="btn btn-primary" value="确定" id="update"/>
                    </div>
                </form>
            </div>
        </div>
    </div>
    <script type="text/javascript">
        //添加
        $(document).ready(function () {
            $('#defaultForm')
                .bootstrapValidator({
                    message: 'This value is not valid',
                    feedbackIcons: {
                        valid: 'glyphicon glyphicon-ok',
                        invalid: 'glyphicon glyphicon-remove',
                        validating: 'glyphicon glyphicon-refresh'
                    },
                    fields: {
                        'WaresCategoryName': {
                            validators: {
                                notEmpty: {
                                    message: '类别名不能为空'
                                },
                                callback: {
                                    message: '类别名已被占用',
                                    callback: function (value, validator) {
                                        var res = true;
                                        if (value != "") {
                                            $.ajax({
                                                url: '@Url.Action("VerificationCName","Wares")',
                                                type: 'post',
                                                dataType: 'text',
                                                async: false,
                                                data: { CName: value },
                                                success: function (data) {
                                                    if (data != "OK")
                                                    {
                                                        res = false;
                                                    }
                                                }
                                            });
                                        }
                                        return res;
                                    }
                                }
                            }
                        },
                        'Describe': {
                            validators: {
                                stringLength: {
                                    max: 25,
                                    message: '类别描述最多25个字符'
                                }
                            }
                        },
                    }
                });
        });
        //修改
         $(document).ready(function () {
            $('#UdefaultForm')
                .bootstrapValidator({
                    message: 'This value is not valid',
                    feedbackIcons: {
                        valid: 'glyphicon glyphicon-ok',
                        invalid: 'glyphicon glyphicon-remove',
                        validating: 'glyphicon glyphicon-refresh'
                    },
                    fields: {
                        'UWaresCategoryName': {
                            validators: {
                                notEmpty: {
                                    message: '类别名不能为空'
                                },
                                callback: {
                                    message: '类别名已被占用',
                                    callback: function (value, validator) {
                                        var res = true;
                                        if (value != "") {
                                            $.ajax({
                                                url: '@Url.Action("UVerificationCName","Wares")',
                                                type: 'post',
                                                dataType: 'text',
                                                async: false,
                                                data: { CName: value, YCName: $("#YCName").val() },
                                                success: function (data) {
                                                    if (data != "OK")
                                                    {
                                                        res = false;
                                                    }
                                                }
                                            });
                                        }
                                        return res;
                                    }
                                }
                            }
                        },
                        'UDescribe': {
                            validators: {
                                stringLength: {
                                    max: 25,
                                    message: '类别描述最多25个字符'
                                }
                            }
                        },
                    }
                });
        });
        //添加类别
        $("#add").click(function () {
            $("#defaultForm").bootstrapValidator('validate');
            if ($("#defaultForm").data("bootstrapValidator").isValid() == true) {
                var CName = $("#WaresCategoryName").val();
                var Describe = $("#Describe").val();
                $.ajax({
                    url: '@Url.Action("AddWaresCategory", "Wares")',
                    type: 'post',
                    dataType: 'text',
                    async: false,
                    data: { CName: CName, Describe: Describe },
                    success: function (data) {
                        if (data != "OK")
                        {
                            alert("添加失败");
                        }
                        else
                        {
                            alert("添加成功");
                            window.location.reload();
                        }
                    }
                });

            }
         });
        //弹出修改类别界面
        function popupdate(CID, CName, Describe, Createdate) {
            $("#UWaresCategoryName").val(CName);
            $("#UDescribe").val(Describe);
            $("#CID").val(CID);
            $("#YCName").val(CName);
            $("#Createdate").val(Createdate);
            $("#UpdateRole").modal();
            //$("#UdefaultForm").bootstrapValidator('validate');
        }
        //修改类别信息
        $("#update").click(function () {
            $("#UdefaultForm").bootstrapValidator('validate');

            if ($("#UdefaultForm").data("bootstrapValidator").isValid() == true) {
                var CName = $("#UWaresCategoryName").val();
                var Describe = $("#UDescribe").val();
                var Createdate = $("#Createdate").val();
                var CID = $("#CID").val();
                $.ajax({
                    url: '@Url.Action("UpdateWaresCategory", "Wares")',
                    type: 'post',
                    dataType: 'text',
                    async: false,
                    data: { CName: CName, Describe: Describe, Createdate: Createdate, CID: CID },
                    success: function (data) {
                        if (data == "NO") {
                            alert("修改失败");
                        }
                        else {
                            alert("修改成功");
                            window.location.reload();
                        }
                    }
                });
            }
        });
        //删除类别
        function del(CID)
        {
            var gnl = confirm("确定要删除?");
            if (gnl == true) {
                $.ajax({
                    url: '@Url.Action("DelWaresCategory", "Wares")',
                    type: 'post',
                    dataType: 'text',
                    async: false,
                    data: { CID: CID },
                    success: function (data) {
                        if (data == "NO") {
                            alert("删除失败");
                        }
                        else {
                            alert("删除成功");
                            window.location.reload();
                        }
                    }
                });
            }
        }
        //置顶类别
        function stick(CID)
        {
            var gnl = confirm("确定要置顶吗?");
            if (gnl == true) {
                $.ajax({
                    url: '@Url.Action("OrderTimeWaresCategory", "Wares")',
                    type: 'post',
                    dataType: 'text',
                    async: false,
                    data: { CID: CID },
                    success: function (data) {
                        if (data == "NO") {
                            alert("置顶失败");
                        }
                        else {
                            alert("置顶成功");
                            window.location.reload();
                        }
                    }
                });
            }
        }
    </script>
</body>
</html>
